<!doctype html>
<html lang="zh-cn">
<head>

    <meta charset="utf-8">
    <meta name="generator" content="Hugo 0.57.2" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Java并发集合框架 | The Sky of OtsWang</title>
    <meta property="og:title" content="Java并发集合框架 - The Sky of OtsWang">
    <meta property="og:type" content="article">
        
    <meta property="article:published_time" content="2019-06-13T18:53:47&#43;08:00">
        
        
    <meta property="article:modified_time" content="2019-06-13T18:53:47&#43;08:00">
        
    <meta name="Keywords" content="golang,go语言,otswang,java,博客,python">
    <meta name="description" content="Java并发集合框架">
        
    <meta name="author" content="OtsWang">
    <meta property="og:url" content="https://otswang.gitee.io/hugo/post/java/juc/collection/">
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">

    <link rel="stylesheet" href="/hugo/css/normalize.css">
    
        <link rel="stylesheet" href="/hugo/css/prism.css">
    
    <link rel="stylesheet" href="/hugo/css/style.css">
    <script type="text/javascript" src="//cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>

    


    
    
</head>

<body>
<header id="header" class="clearfix">
    <div class="container">
        <div class="col-group">
            <div class="site-name ">
                
                    <a id="logo" href="https://otswang.gitee.io/hugo/">
                        The Sky of OtsWang
                    </a>
                
                <p class="description">擅长写HelloWorld的小小码农</p>
            </div>
            <div>
                <nav id="nav-menu" class="clearfix">
                    
                    
                    <a  href="https://otswang.gitee.io/hugo/" title="Home">Home</a>
                    
                    <a  href="https://otswang.gitee.io/hugo/tags/" title="Tags">Tags</a>
                    
                    <a  href="https://otswang.gitee.io/hugo/categories/" title="Categories">Categories</a>
                    
                    <a  href="https://otswang.gitee.io/hugo/archives/" title="Archives">Archives</a>
                    
                    <a  href="https://otswang.gitee.io/hugo/about/" title="About">About</a>
                    
                </nav>
            </div>
        </div>
    </div>
</header>


<div id="body">
    <div class="container">
        <div class="col-group">

            <div class="col-8" id="main">
                <div class="res-cons">
                    <article class="post">
                        <header>
                            <h1 class="post-title">Java并发集合框架</h1>
                        </header>
                        <date class="post-meta meta-date">
                            2019年6月13日
                        </date>
                        
                        <div class="post-meta">
                            <span>|</span>
                            
                                <span class="meta-category"><a href="https://otswang.gitee.io/hugo/categories/java-juc">Java-juc</a></span>
                            
                        </div>
                        
                        
                        
                        <div class="post-content">
                            <p>Jdk 中的集合框架的父接口是Iterable，依次为Collection接口，List+Queue+Set接口。</p>

<h2 id="collection">Collection</h2>

<pre><code>package
  java.util

interface
  Collection

method
  size [Collection]
  isEmpty [Collection]
  contains [Collection]
  iterator [Collection]
  toArray [Collection]
  toArray [Collection]
  add [Collection]
  remove [Collection]
  containsAll [Collection]
  addAll [Collection]
  removeAll [Collection]
  removeIf [Collection]
  retainAll [Collection]
  clear [Collection]
  equals [Collection]
  hashCode [Collection]
  spliterator [Collection]
  stream [Collection]
  parallelStream [Collection]
</code></pre>

<h3 id="list">List</h3>

<p>List接口最常见的非并发实现类是ArrayList，它是非线程安全的。线程安全的实现类是Vector，Vector的子类Stack是一个实现了先进后出的对象堆栈。</p>

<h3 id="set">Set</h3>

<p>Set的特点是内容不允许重复，防止重复需要重写hashCode()和equals()方法。Set的常用实现类是HashSet，它是非线程安全且无序的，LinkedHashSet类是有序实现类。TreeSet也是实现了自动排序的实现类。LinkedHashSet 与 TreeSet的区别在于：LinkedHashSet使用的是插入的顺序，而TreeSet是自动排序的。</p>

<h3 id="queue">Queue</h3>

<p>Queue的并发实现类为PriorityQueue， 是一个基于优先级的无界优先级队列。</p>

<h3 id="deque">Deque</h3>

<p>Deque为双端队列。Deque实现了Queue接口。其非并发实现类有ArrayDeque和LinkedList</p>

<h2 id="非阻塞队列">非阻塞队列</h2>

<p>当队列中没有数据时，操作队列会出现异常或返回null，不具有等待阻塞的功能。</p>

<ul>
<li>ConcurrentHashMap</li>
<li>ConcurrentSkipListMap</li>
<li>ConcurrentSkipListSet</li>
<li>ConcurrentLinkedQueue</li>
<li>ConcurrentLinkedDeque</li>
<li>CopyOnWriteArrayList  ArrayList的线程安全</li>
<li>CopyOnWriteArraySet   HashSet的线程安全</li>
</ul>

<h2 id="阻塞队列">阻塞队列</h2>

<p>阻塞队列BlockingQueue，其特点是当BlockingQueue是空的时候，如果从中取数据，操作会阻塞进入等待状态，直到BlockingQueue中添加了新的元素才会被唤醒，同样，如果BlockingQueue是满的，添加新的元素会阻塞进入等待状态，直到有元素被消费才唤醒。</p>

<p>BlockingQueue的常见操作：</p>

<pre><code> package
   java.util.concurrent

 interface
   BlockingQueue

 method
   add [BlockingQueue]
   offer [BlockingQueue]
   put [BlockingQueue]      // 阻塞的加入
   offer [BlockingQueue]
   take [BlockingQueue]     // 阻塞的获取
   poll [BlockingQueue]
   remainingCapacity [BlockingQueue]
   remove [BlockingQueue]
   contains [BlockingQueue]
   drainTo [BlockingQueue]
   drainTo [BlockingQueue]
</code></pre>

<p>实现类：</p>

<p>ArrayBlockingQueue，有界的</p>

<p>PriorityBlockingQueue</p>

<p>LinkedBlockingQueue， 无界的，与ArrayBlockingQueue类似</p>

<p>LinkedBlockingDeque</p>

<p>SynchronousQueue，异步队列，有一个加入后会立刻阻塞，必须有一个取出才唤醒</p>

<p>DelayQueue，元素需要实现Delayed接口</p>

<p>LinkedTransferQueue，与SynchronousQueue类似，但是它有可能有一个元素的容量。transfer()方法，可以将一个元素加入到队列中，或者有消费者时直接被消费。</p>
                        </div>

                        


                        

<div class="post-archive">
    <h2>See Also</h2>
    <ul class="listing">
        
        <li><a href="/hugo/post/java/juc/skywang_learn/">Skywang_learn</a></li>
        
    </ul>
</div>


                        <div class="post-meta meta-tags">
                            
                            <ul class="clearfix">
                                
                                <li><a href="https://otswang.gitee.io/hugo/tags/juc">juc</a></li>
                                
                                <li><a href="https://otswang.gitee.io/hugo/tags/%E5%B9%B6%E5%8F%91%E9%9B%86%E5%90%88">并发集合</a></li>
                                
                            </ul>
                            
                        </div>
                    </article>
                    
    

    
    
                </div>
            </div>
            <div id="secondary">

    <section class="widget">
        <form id="search" action="//www.google.com/search" method="get" accept-charset="utf-8" target="_blank" _lpchecked="1">
      
      <input type="text" name="q" maxlength="20" placeholder="Search">
      <input type="hidden" name="sitesearch" value="https://otswang.gitee.io/hugo/">
      <button type="submit" class="submit icon-search"></button>
</form>
    </section>

    
    <div class="clear">
        <div class="toc-article">
            <div class="toc-title">文章目录</dixsv>
            <nav id="TableOfContents">
<ul>
<li>
<ul>
<li><a href="#collection">Collection</a>
<ul>
<li><a href="#list">List</a></li>
<li><a href="#set">Set</a></li>
<li><a href="#queue">Queue</a></li>
<li><a href="#deque">Deque</a></li>
</ul></li>
<li><a href="#非阻塞队列">非阻塞队列</a></li>
<li><a href="#阻塞队列">阻塞队列</a></li>
</ul></li>
</ul>
</nav>
        </div>
    </div>
    

</div>
        </div>
    </div>
</div>
<footer id="footer">
    <div class="container">
        &copy; 2020 <a href="https://otswang.gitee.io/hugo/">The Sky of OtsWang By OtsWang</a>.
        Powered by <a rel="nofollow noreferer noopener" href="https://gohugo.io" target="_blank">Hugo</a>.
        <a href="https://www.flysnow.org/" target="_blank">Theme</a> based on <a href="https://github.com/Dudiao137/maupassant-hugo" target="_blank">maupassant-ots</a>.
        
    </div>
</footer>


    <script type="text/javascript">
    
    (function(){
        $("pre code").parent().addClass("line-numbers")
    }())

    window.MathJax = {
        tex2jax: {
            inlineMath: [ ['$','$'] ],
            processEscapes: true
        }
    };
    </script>
    <script type="text/javascript" src="/hugo/js/prism.js" async="true"></script>
    <script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML' async></script>

<a id="rocket" href="#top"></a>
<script type="text/javascript" src="/hugo/js/totop.js?v=0.0.0" async=""></script>







 
 <script src="https://mermaidjs.github.io/scripts/mermaid.min.js"></script>
 <script>
       mermaid.initialize({ startOnLoad: true });
 </script>
</body>
</html>
